clear;clc;

load Factors FF3 Rf yyyymm;
TBeg = find(yyyymm == 201112); yyyymm = yyyymm(TBeg:end); T = length(yyyymm); 
FF3 = FF3(TBeg:end,:); Rf = Rf(TBeg:end);
clear TBeg T;

WhichPort = 1; % 1=WithMicro, 2=NoMicro, 3=s20, 4=s50

switch WhichPort
    case 1, load Portfolios_MicroYes; PortIndx = 1:10;
    case 2, load Portfolios_MicroNo; PortIndx = 1:10;
    case 3, load Portfolios_s20; PortIndx = [1 10];
    case 4, load Portfolios_s50; PortIndx = [1 10];
end

AlphaG = NaN(8,11); AlphaG_T = NaN(8,11);
AlphaN = NaN(8,11,8); AlphaN_T = NaN(8,11,8);

X = [ones(120,1) FF3(2:end,1)];
for portnum = 1:8

    if portnum<=6
        RPort = Port(portnum).RetPort(:,:,2);
    elseif portnum==7
        RPort = Port(1).RetPort(:,:,2);
        for ii = 2:4
            RPort = RPort + Port(ii).RetPort(:,:,2);
        end
        RPort = RPort/4;
    elseif portnum==8
        RPort = Port(1).RetPort(:,:,2);
        for ii = 2:6
            RPort = RPort + Port(ii).RetPort(:,:,2);
        end
        RPort = RPort/6;
    end
    RGross = RPort - Rf;

    RNet = NaN(121,10,8);
    RNet(:,:,1) = RGross - Port(portnum).PIContinuous_YesIntg;
    RNet(:,:,2) = RGross - Port(portnum).PIContinuous_NotIntg;
    RNet(:,:,3) = RGross - Port(portnum).PIClosing_YesIntg;
    RNet(:,:,4) = RGross - Port(portnum).PIClosing_NotIntg;
    RNet(:,:,5) = RGross - Port(portnum).PIHybrid_YesIntg;
    RNet(:,:,6) = RGross - Port(portnum).PIHybrid_NotIntg;
    RNet(:,:,7) = RGross - Port(portnum).PIOpening_YesIntg;
    RNet(:,:,8) = RGross - Port(portnum).PIOpening_NotIntg;
    
    RGross(:,11) = RGross(:,10) - RGross(:,1);
    RNet(:,11,1) = RGross(:,11) - Port(portnum).PIContinuous_YesIntg(:,10) - Port(portnum).PIContinuous_YesIntg(:,1);
    RNet(:,11,2) = RGross(:,11) - Port(portnum).PIContinuous_NotIntg(:,10) - Port(portnum).PIContinuous_NotIntg(:,1);
    RNet(:,11,3) = RGross(:,11) - Port(portnum).PIClosing_YesIntg(:,10) - Port(portnum).PIClosing_YesIntg(:,1);
    RNet(:,11,4) = RGross(:,11) - Port(portnum).PIClosing_NotIntg(:,10) - Port(portnum).PIClosing_NotIntg(:,1);
    RNet(:,11,5) = RGross(:,11) - Port(portnum).PIHybrid_YesIntg(:,10) - Port(portnum).PIHybrid_YesIntg(:,1);
    RNet(:,11,6) = RGross(:,11) - Port(portnum).PIHybrid_NotIntg(:,10) - Port(portnum).PIHybrid_NotIntg(:,1);
    RNet(:,11,7) = RGross(:,11) - Port(portnum).PIOpening_YesIntg(:,10) - Port(portnum).PIOpening_YesIntg(:,1);
    RNet(:,11,8) = RGross(:,11) - Port(portnum).PIOpening_NotIntg(:,10) - Port(portnum).PIOpening_NotIntg(:,1);

    for p = 1:11
        Y = RGross(2:end,p);
        [B,Tols,Tnwest,AR2] = myols(X,Y,2);
        AlphaG(portnum,p) = B(1); AlphaG_T(portnum,p) = Tols(1);

        for nn = 1:8
            Y = RNet(2:end,p,nn);
            [B,Tols,Tnwest,AR2] = myols(X,Y,2);
            AlphaN(portnum,p,nn) = B(1); AlphaN_T(portnum,p,nn) = Tols(1);
        end
    end
end

AlphaG = 1200*AlphaG; AlphaN = 1200*AlphaN;
X =[AlphaG NaN(8,12); NaN(1,23);
    AlphaN(:,:,1) NaN(8,1) AlphaN(:,:,2); NaN(1,23); % continuous
    AlphaN(:,:,3) NaN(8,1) AlphaN(:,:,4); NaN(1,23); % closing
    AlphaN(:,:,5) NaN(8,1) AlphaN(:,:,6); NaN(1,23); % hybrid
    AlphaN(:,:,7) NaN(8,1) AlphaN(:,:,8)]; % opening
prettymat(X);
